package com.hr.minio;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@RestController
@Api(tags = "文件操作")
@RequiredArgsConstructor
public class FileController {
    private final MinioClient minioClient;
    @Value("${minio.bucket}")
    public String bucket;
    @Value("${minio.url}")
    private String baseurl;
    @PostMapping("upload1")
    public String upload1(@RequestPart MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        if(!file.isEmpty()){
//            System.err.println(minioClient.bucketExists(BucketExistsArgs.builder().bucket("xingnetstudy").build()));
            //上传
            minioClient.putObject(PutObjectArgs.builder().
                    bucket(bucket). //桶的名称
                            object(file.getOriginalFilename()). //对象名
                            stream(file.getInputStream(),file.getSize(),-1). //文件内容 参数说明:1.文件内容 流 2.文件大小 3.分片
                            contentType(file.getContentType()).//设置文件的类型
                            build());
//            //返回  文件的访问地址
            return "http://127.0.0.1:9000/hrwork/"+file.getOriginalFilename();
        }
        return "fail";
    }
}